### Settings
CC			= gcc
CXX			= g++
CFLAGS		= -O2 -pipe -Wno-unused-result 
CXXFLAGS	= $(CFLAGS) -std=c++11
INCS		= -I./_depends/libmicrohttpd -I./_depends/luajit
LDFLAGS		= -L./_depends/libmicrohttpd -L./_depends/luajit -lmicrohttpd -lluajit -lgnutls -lgcrypt -lpthread -luuid -ldl
SHELL		= /bin/bash

OUTPUT		= ../bin/omni
SRCDIR		= . apis utils
OBJDIR		= ../obj/linux64

### Auto-Generate
SRCS	= $(foreach path, $(SRCDIR), $(wildcard $(path)/*.cc) $(wildcard $(path)/*.c))
OBJS	= $(patsubst %.c, $(OBJDIR)/%.o, $(patsubst %.cc, $(OBJDIR)/%.o, $(SRCS)))
DIRS	= $(sort $(patsubst %/, %, $(dir $(OBJS))))

### Targets
all: prebuild build

prebuild:
	@echo -e "\033[36m=== Start to build $(OUTPUT) ===\033[0m"
	@test -d '$(DIRS)' | mkdir -p $(DIRS)

build: $(OBJS)
	@echo -e "\033[36m>>> Generate $(OUTPUT) ...\033[0m"
	@$(CXX) -o $(OUTPUT) $(OBJS) $(LDFLAGS)

clean:
	@rm -f $(OBJS) $(OUTPUT)

### Rules
$(OBJDIR)/%.o: %.cc
	@echo -e "\033[33m---> Compile $< ...\033[0m"
	@$(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)

$(OBJDIR)/%.o: %.c
	@echo -e "\033[33m---> Compile $< ...\033[0m"
	@$(CC) $(CFLAGS) -c $< -o $@ $(INCS)
